package com.om.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;



import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;

public class MemcacheUtils {

	/**
	 * 保存
	 * */
	public static boolean saveMemcache(String key,Object object,int time){
		
		MemcachedClient m = MemcacheUtils.getMemCachedClient();
		
		try {
			boolean b  =false;
			if(object!=null){
				m.set(key,time,object);
			}
				
			return b;
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MemcachedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				m.shutdown();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
	/**
	 * 删除
	 * */
	public static boolean delMemcache(String key){

		MemcachedClient m = MemcacheUtils.getMemCachedClient();
		try {
			boolean b = m.delete(key);
			return b;
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MemcachedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				m.shutdown();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
	/**
	 * 查询获取
	 * */
	public static Object getMemcacheValue(String key){
		MemcachedClient m = MemcacheUtils.getMemCachedClient();
		try {
			return m.get(key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				m.shutdown();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	public static MemcachedClient getMemCachedClient(){
		try {
			MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.200.211:11211"));
			MemcachedClient memcachedClient = builder.build();
			
//			memcachedClient.set("hello", 0, "Hello,xmemcached");
//			String value = memcachedClient.get("ab");
//			System.out.println("hello=" + value);
//			//memcachedClient.delete("hello");
//			//value = memcachedClient.get("hello");
//			//System.out.println("hello=" + value);
//			memcachedClient.shutdown();
			return memcachedClient;
			} catch (Exception e) {
			System.err.println("Shutdown MemcachedClient fail");
			e.printStackTrace();
			}
			return null;
	}
	/*public static void main(String[] args) {
//		try {
//			MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.200.211:11211"));
//			MemcachedClient memcachedClient = builder.build();
//			
//			//memcachedClient.set("hello", 0, "Hello,xmemcached");
//			String value = memcachedClient.get("hello");
//			System.out.println("hello=" + value);
//			//memcachedClient.delete("hello");
//			//value = memcachedClient.get("hello");
//			//System.out.println("hello=" + value);
//			memcachedClient.shutdown();
//			} catch (Exception e) {
//			System.err.println("Shutdown MemcachedClient fail");
//			e.printStackTrace();
//			}
		Map map = new HashMap();
		map.put("1", "111");
		map.put("2", "222");
		map.put("3", "333");
		
		
		//System.out.println(MemcachePhp.saveMemcache("ah",map,0));
		Map obj = (Map)MemcacheUtils.getMemcacheValue("ah");
		System.out.println(obj.get("2"));
		//System.out.println(MemcachePhp.delMemcache("ah"));
		
	}*/
}
